﻿//-----------------------------------------------------------------------
// <copyright file="UpdateFile.Data.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System;
using System.Data;
using GateShift.Common.Repository;
using GateShift.Common.Repository.Interfaces.DataInterfaces.UpdateData;

#endregion

namespace GateShift.Servers.WCFHost.UpdateData.Entities
{
    /// <summary>
    /// Data class for UpdateFile Data
    /// </summary>
    public partial class UpdateFile : IUpdateFileData
    {
        #region State Management

        /// <summary>
        /// Gets a value indicating whether or not this instance has been modified
        /// </summary>
        public bool IsDirty
        {
            get
            {
                if (IsNew)
                {
                    return true;
                }

                return EntityState == EntityState.Modified;
            }
        }

        /// <summary>
        /// Gets a value indicating whether or not this instance is new
        /// </summary>
        public bool IsNew
        {
            get
            {
                // Before it is attached if the key is null then we are new
                if (EntityState == EntityState.Detached)
                {
                    if (EntityKey == null)
                    {
                        return true;
                    }
                }

                return EntityState == EntityState.Added;
            }
        }

        /// <summary>
        /// Gets a value indicating whether or not this instance has been deleted
        /// </summary>
        public bool IsDeleted
        {
            get
            {
                return EntityState == EntityState.Deleted;
            }
        }

        #endregion

        #region Identity Management

        /// <summary>
        /// Gets an Identity based reference for this item
        /// </summary>
        public Identity ID
        {
            get
            {
                return new Identity(FileID);
            }
        }

        #endregion

        #region ID Reference Properties

        /// <summary>
        /// Gets or sets the associated Release ID for this file
        /// </summary>
        public Identity? ReleaseID
        {
            get
            {
                return (Identity?)(UpdateReleaseReference.EntityKey == null ? null : UpdateReleaseReference.EntityKey.EntityKeyValues[0].Value);
            }

            set
            {
                if (value.HasValue)
                {
                    throw new ArgumentNullException("value", "ReleaseID must have a value");
                }

                if (!value.Value.CanFitInInt16())
                {
                    throw new InvalidCastException("Identity is not of type short ");
                }

                UpdateReleaseReference.EntityKey = new EntityKey("EntityDataAdapter.UpdateReleases", "UpdateReleaseID", Convert.ToInt16(value.Value));
            }
        }

        /// <summary>
        /// Gets or sets the associated Map ID for this file
        /// </summary>
        public Identity? MapID
        {
            get
            {
                return (Identity?)(UpdateMapReference.EntityKey == null ? null : UpdateMapReference.EntityKey.EntityKeyValues[0].Value);
            }

            set
            {
                if (value.HasValue)
                {
                    if (!value.Value.CanFitInInt16())
                    {
                        throw new InvalidCastException("Identity is not of type short ");
                    }

                    UpdateMapReference.EntityKey = new EntityKey("EntityDataAdapter.UpdateMaps", "UpdateMapID", Convert.ToInt16(value.Value));
                }
                else
                {
                    UpdateMapReference.EntityKey = null;
                }
            }
        }

        #endregion

        #region Validation

        /// <summary>
        /// Handles validating of FileName being changed
        /// </summary>
        /// <param name="value">Value to validate</param>
        partial void OnFileNameChanging(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new NullReferenceException("File Name cannot be null or empty");
            }

            if (value.Length > 1024)
            {
                throw new IndexOutOfRangeException("File Name cannot exceed 1024 characters");
            }
        }

        /// <summary>
        /// Handles validating of Hash being changed
        /// </summary>
        /// <param name="value">Value to validate</param>
        partial void OnHashChanging(byte[] value)
        {
            if (value == null)
            {
                throw new NullReferenceException("Hash cannot be null");
            }

            if (value.Length > 128)
            {
                throw new IndexOutOfRangeException("Hash cannot exceed 128 characters");
            }
        }

        #endregion
    }
}